"use strict";
// 角色模块
const BaseController = require("./base");
const TemplateDto = require("../dto/template.dto");
class TemplateController extends BaseController {
  /**
   * @description 分页查询列表
   * @param {TemplateDto.pageDto} 查询参数
   */
  async page() {
    try {


      // **可封装为verifytParams函数开始**

      // 获取必传参数结构
      const { requiredKeys, paramKeys } = this.ctx.helper.getRequiredKey(
        TemplateDto.pageDto
      );
      // 校验必传参数
      const checkRequiredRes = this.ctx.helper.checkRequired(
        this.ctx.query,
        requiredKeys
      );
      if (checkRequiredRes) throw checkRequiredRes;
      // 生成请求参数
      const params = this.ctx.helper.createParams(this.ctx.query, paramKeys);
      
      // **可封装为verifytParams函数结束**


      const res = await this.ctx.service.template.page(params);
      if (!res.success) throw res.msg;
      await this.listSuccess(res.data.rows, res.data.count);
    } catch (err) {
      await this.error(err);
    }
  }

  /**
   * @description 创建
   * @param {TemplateDto.createDto} 参数
   */
  async create() {
    try {
      // 获取必传参数结构
      const { requiredKeys, paramKeys } = this.ctx.helper.getRequiredKey(
        TemplateDto.createDto
      );
      // 校验必传参数
      const checkRequiredRes = this.ctx.helper.checkRequired(
        this.ctx.request.body,
        requiredKeys
      );
      if (checkRequiredRes) throw checkRequiredRes;
      // 生成请求参数
      let params = this.ctx.helper.createParams(
        this.ctx.request.body,
        paramKeys
      );
      params.createTime = new Date();
      params.createUser = this.ctx.header.account;
      const res = await this.service.template.create(params);
      if (!res.success) throw res.msg;
      await this.success(params);
    } catch (err) {
      await this.error(err);
    }
  }

  /**
  * @description 修改
  * @param {TemplateDto.updateDto}
  */
  async update() {
    try {
      // 获取必传参数结构
      const { requiredKeys, paramKeys } = this.ctx.helper.getRequiredKey(
        TemplateDto.updateDto
      );
      // 校验必传参数
      const checkRequiredRes = this.ctx.helper.checkRequired(
        this.ctx.request.body,
        requiredKeys
      );
      if (checkRequiredRes) throw checkRequiredRes;
      // 生成请求参数
      let params = this.ctx.helper.createParams(
        this.ctx.request.body,
        paramKeys
      );
      params.updateTime = new Date();
      params.updateUser = this.ctx.header.account;
      const id = params.id;
      delete params.id;
      const res = await this.service.template.update(id, params);
      if (!res.success) throw res.msg;
      await this.success();
    } catch (err) {
      await this.error(err);
    }
  }

  /**
   * @description 删除
   * @param {TemplateDto.delDto}
   */
   async del() {
    try {
      // 获取必传参数结构
      const { requiredKeys, paramKeys } = this.ctx.helper.getRequiredKey(
        TemplateDto.delDto
      );
      // 校验必传参数
      const checkRequiredRes = this.ctx.helper.checkRequired(
        this.ctx.query,
        requiredKeys
      );
      if (checkRequiredRes) throw checkRequiredRes;
      const params = this.ctx.helper.createParams(
        this.ctx.query,
        paramKeys
      );
      const res = await this.service.template.del(params)
      if(!res.success) throw res.msg;
      await this.success();
    } catch (err) {
      await this.error(err);
    }
  }
}

module.exports = TemplateController;
